package com.eclipse.organigram.views.ui;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import com.eclipse.organigram.model.OrganigramModel;
import com.eclipse.organigram.model.entities.TreeElement;

public class OrganigramContentProvider implements ITreeContentProvider {

	private OrganigramModel organigramModel;

	public OrganigramContentProvider(OrganigramModel organigramModel) {
		this.organigramModel = organigramModel;
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		organigramModel = (OrganigramModel) newInput;
	}

	@Override
	public Object[] getElements(Object inputElement) {
		return organigramModel.getCategories().toArray();
	}

	@Override
	public Object[] getChildren(Object parent) {
		if (parent instanceof TreeElement) {
			return ((TreeElement) parent).getChildren();
		}
		return null;
	}

	@Override
	public Object getParent(Object element) {
		return ((TreeElement) element).getParent();
	}

	@Override
	public boolean hasChildren(Object element) {
		if (element instanceof TreeElement) {
			return ((TreeElement) element).hasChildren();
		}
		return false; 
	}

	@Override
	public void dispose() {
	}
	
}
